package com.fau.cm.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCallCustomerReminder;
import com.fau.cm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc  客户提醒Action
 * @author yangzj
 * @datetime 2014年10月14日,下午3:11:20
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class CustomerRemindAction extends PublicAction implements ModelDriven<MedicalCallCustomerReminder>{
	
	private static final long serialVersionUID = -3121131576990557533L;
	
	private MedicalCallCustomerReminder reminder = new MedicalCallCustomerReminder();
	
	/**
	 * @desc 跳转到客户提醒页面
	 * @return SUCCESS
	 * @author yangzj
	 * @datetime 2014年10月14日,下午3:15:22
	 */
	@Action(value = "customer_remind_init", results = {@Result(location = "customer_remind_init.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 跳转到编辑页面
	 * @return SUCCESS
	 * @author yangzj
	 * @datetime 2014年10月17日,下午9:40:41
	 */
	@Action(value = "reminder_edit", results = {@Result(location = "reminder_edit.jsp")})
	public String edit(){
		return SUCCESS;
	}

	/**
	 * @desc 分页查询客户提醒
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月17日,下午8:48:01
	 */
	@Action(value = "customer_remind_view")
	public String view(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getCustomerService().findReminderByPage(admin, reminder, map);
		this.map.clear();
		this.map.put("total", this.pageBean.getTotalRow());
		this.map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 保存修改
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月17日,下午10:21:18
	 */
	@Action(value = "reminder_save_edit")
	public String saveEdit(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		this.baseAllService.getCustomerService().editSave(this.reminder);
		this.map.put("success", true);
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 删除客户提醒
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月18日,上午10:56:46
	 */
	@Action(value = "remind_delete")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(reminder.getId())){
			this.baseAllService.getCustomerService().deleteRemind(reminder);
			this.map.put("success", true);
		}else{
			this.map.put("error", true);
		}
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 导出全部客户提醒
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月17日,下午10:40:59
	 */
	@Action(value = "remind_export")
	public String export() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"客户提醒信息明细.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getCustomerService().exportRemind(admin);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @desc 导出当前页客户提醒
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月17日,下午10:40:43
	 */
	@Action(value = "remind_current_export")
	public String currentExport(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"客户提醒信息明细.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getCustomerService().currentExportRemind(admin, reminder.getIds());
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 打开任务页面，跳转到客户提醒页面
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月5日,上午9:31:21
	 */
	@Action(value = "customer_remind", results = {@Result(location = "customer_remind.jsp")})
	public String customeRemind() {
		return SUCCESS;
	}
	
	/**
	 * @desc 根据客户id查询客户提醒
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月5日,上午9:27:52
	 */
	@Action(value = "customer_remind_find")
	public String findCustomeRemindByCId() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getCustomerService().findCustomeRemindByCId(reminder.getCustomerId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 保存客户提醒
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月5日,上午10:13:30
	 */
	@Action(value = "customer_remind_save")
	public String save() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		reminder.setOperatorId(admin.getId());
		reminder.setHospitalId(admin.getEnterpriseId());
		this.baseAllService.getCustomerService().saveCustomeRemind(reminder);
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	@Override
	public MedicalCallCustomerReminder getModel() {
		return reminder;
	}
}
